import {findList} from 'services/message'
import modelExtend from "dva-model-extend"
import {pageModel} from 'utils/model'

export default modelExtend(pageModel, {
  namespace: 'message',

  state: {
    list: [],
    modalVisible: false,
  },

  effects: {

    * findList ({payload}, {call, put}) {
      const {code, msg, data} = yield call(findList, payload)
      if (code === 0) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.records,
            pagination: {
              current: Number(payload.current) || 1,
              pageSize: Number(payload.size) || 10,
              total: data.total,
            },
          },
        })
      } else {
        throw {code, message: msg}
      }
    },

  },

  reducers: {

    showModal (state, {payload}) {
      return {...state, ...payload, modalVisible: true}
    },

    hideModal (state, {payload}) {
      return {...state, ...payload, modalVisible: false}
    },

    updatePagination (state, {payload}) {
      let pagination = {
        ...state.pagination,
        current: Number(payload.current) || state.pagination.current,
        pageSize: Number(payload.size) || state.pagination.pageSize,
      }
      return {...state, pagination}
    },

  },
})
